package com.lmscn.lms.api.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	public static String getYear() {
		return new SimpleDateFormat("yyyy").format(new Date()).toString();
	}

	public static String getMonth() {
		return new SimpleDateFormat("MM").format(new Date()).toString();
	}

	public static String getDay() {
		return new SimpleDateFormat("dd").format(new Date()).toString();
	}

	public static String getWeek() {
		int year = Integer.valueOf(getYear());
		int month = Integer.valueOf(getMonth());
		int day = Integer.valueOf(getDay());
		return getWeek(year, month, day);
	}

	public static String getWeek(int y, int m, int d) {
		if (m == 1) {
			m = 13;
			y--;
		}
		if (m == 2) {
			m = 14;
			y--;
		}
		int week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7;
		String weekstr = "";
		switch (week) {
		case 0:
			weekstr = "星期一";
			break;
		case 1:
			weekstr = "星期二";
			break;
		case 2:
			weekstr = "星期三";
			break;
		case 3:
			weekstr = "星期四";
			break;
		case 4:
			weekstr = "星期五";
			break;
		case 5:
			weekstr = "星期六";
			break;
		case 6:
			weekstr = "星期日";
			break;
		}
		return weekstr;
	}

	public static Date getWeekBegin() {
		int year = Integer.valueOf(getYear());
		int month = Integer.valueOf(getMonth());
		int day = Integer.valueOf(getDay());
		return getWeekBegin(year, month, day);
	}

	public static Date getWeekBegin(int y, int m, int d) {
		if (m == 1) {
			m = 13;
			y--;
		}
		if (m == 2) {
			m = 14;
			y--;
		}
		int week = (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7;
		String weekstr = "";
		Date date = new Date();
		Date weekBegin = new Date(date.getTime() - week * 60 * 60 * 24 * 1000);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		try {
			weekBegin = sdf.parse(sdf.format(weekBegin));
		} catch (ParseException e) {
			e.printStackTrace();
		}
		/*weekBegin = new Date(weekBegin.getYear(), weekBegin.getMonth(),
				weekBegin.getDay());*/
		return weekBegin;
	}

	public static String replaceBR(String s) {
		if (s != null) {
			s = s.replace("\n", "");
			s = s.replace("\r", "");
			s = s.replace("\t", " ");
		}
		return s;
	}

	public static String htmEncode(String s) {
		StringBuffer stringbuffer = new StringBuffer();
		int j = s.length();
		for (int i = 0; i < j; i++) {
			char c = s.charAt(i);
			switch (c) {
			case 60:
				stringbuffer.append("&lt;");
				break;
			case 62:
				stringbuffer.append("&gt;");
				break;
			case 38:
				stringbuffer.append("&amp;");
				break;
			case 34:
				stringbuffer.append("&quot;");
				break;
			case 169:
				stringbuffer.append("&copy;");
				break;
			case 174:
				stringbuffer.append("&reg;");
				break;
			case 165:
				stringbuffer.append("&yen;");
				break;
			case 8364:
				stringbuffer.append("&euro;");
				break;
			case 8482:
				stringbuffer.append("&#153;");
				break;
			case 13:
				if (i < j - 1 && s.charAt(i + 1) == 10) {
					stringbuffer.append("<br>");
					i++;
				}
				break;
			case 32:
				if (i < j - 1 && s.charAt(i + 1) == ' ') {
					stringbuffer.append(" &nbsp;");
					i++;
					break;
				}
			default:
				stringbuffer.append(c);
				break;
			}
		}
		return new String(stringbuffer.toString());
	}

	public static Date getMonthBegin() {
		Date date = new Date();
		Date weekBegin = new Date();
		weekBegin = new Date(date.getYear(), date.getMonth(), 1);
		return weekBegin;
	}
	/**
	 * 计算两个日期之间相差的分钟
	 * 张盈盈
	 * @param dateBegin
	 * @param dateEnd
	 * @return
	 */
	public long calcDate(String dateBegin, String dateEnd){            // 时间之间的天数
		 long quot = 0;
		  SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		  try {
		   Date date1 = ft.parse( dateBegin);
		   Date date2 = ft.parse( dateEnd );
		   quot = date2.getTime() - date1.getTime();
		   quot = quot / 1000 / 60 ;
		  } catch (ParseException e) {
		   e.printStackTrace();
		  }
		  return quot;
	}
	
	/**
	 * 把数字月份转化为汉字月份
	 * */
	public static String getMonth(String month){
		String monthString = "";
		if(month.equals("1")){
			monthString = "一月";
		}else if(month.equals("2")){
			monthString = "二月";
		}else if(month.equals("3")){
			monthString = "三月";
		}else if(month.equals("4")){
			monthString = "四月";
		}else if(month.equals("5")){
			monthString = "五月";
		}else if(month.equals("6")){
			monthString = "六月";
		}else if(month.equals("7")){
			monthString = "七月";
		}else if(month.equals("8")){
			monthString = "八月";
		}else if(month.equals("9")){
			monthString = "九月";
		}else if(month.equals("10")){
			monthString = "十月";
		}else if(month.equals("11")){
			monthString = "十一月";
		}else if(month.equals("12")){
			monthString = "十二月";
		}else{
			monthString = month;
		}
		return monthString;
	}
	
	/**
	 * 取得两个时间段的时间间隔 return t2 与t1的间隔天数 throws ParseException
	 * 如果输入的日期格式不是0000-00-00 格式抛出异常
	 */
	public static int getBetweenDays(String t1, String t2)
			throws ParseException {
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		int betweenDays = 0;
		Date d1 = format.parse(t1);
		Date d2 = format.parse(t2);
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		c1.setTime(d1);
		c2.setTime(d2);
		// 保证第二个时间一定大于第一个时间
		if (c1.after(c2)) {
			c1 = c2;
			c2.setTime(d1);
		}
		int betweenYears = c2.get(Calendar.YEAR) - c1.get(Calendar.YEAR);
		betweenDays = c2.get(Calendar.DAY_OF_YEAR)
				- c1.get(Calendar.DAY_OF_YEAR);
		for (int i = 0; i < betweenYears; i++) {
			c1.set(Calendar.YEAR, (c1.get(Calendar.YEAR) + 1));
			betweenDays += c1.getMaximum(Calendar.DAY_OF_YEAR);
		}
		return betweenDays;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// System.out.println(DateUtil.return2bit(2));
		System.out.println(getWeek());
		System.out.println(getMonthBegin());

		System.out.print(htmEncode("<p>用来测试排序的课程</p>"));
	}
	
	/**
	 * 得到系统日期的前一天
	 */
	public static String getSysBeforeDay(){
		
		Calendar calendar = Calendar.getInstance(); 
		calendar.setTime(new Date());
		calendar.add(Calendar.DAY_OF_MONTH, -1); 
		Date dateBefore = calendar.getTime();
		
		DateFormat simpl = new SimpleDateFormat("yyyy-MM-dd");
		return simpl.format(dateBefore);
	}
}
